package com.kkshop.action;

import org.apache.log4j.Logger;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;

import com.kkshop.model.security.Authority;
import com.kkshop.model.security.User;
import com.kkshop.service.PersistenceService;
import com.kkshop.service.TransactionService;
import com.kkshop.util.UserUtils;
import com.kkshop.xwork2.AjaxActionSupport;
import com.opensymphony.xwork2.Action;

public class RegisterAction extends AjaxActionSupport {
    
    private static final Logger log = Logger.getLogger(RegisterAction.class);
    
    private PersistenceService persistenceService;
    private TransactionService transactionService;
    private UserUtils userUtils;
    
    private String username;
    private String password;
    
    public String registerCustomer() {
        return register(username, password, "ROLE_CUSTOMER");
    }
    
    public String registerPartner() {
        return register(username, password, "ROLE_PARTNER");
    }
    
    private String register(final String username, final String password, final String role) {
        return (String) transactionService.doInTransaction(new TransactionCallback() {
            public Object doInTransaction(TransactionStatus status) {
                User newUser = new User();
                newUser.setUsername(username);
                newUser.setPassword(password);
                newUser.setEnabled(1);
                try {
                    persistenceService.save(newUser);
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    status.setRollbackOnly();
                    return result(Action.ERROR, getText("registration.error"));
                }
                
                Authority newAuthority = new Authority();
                newAuthority.setAuthority(role);
                newAuthority.setUsername(username);
                try {
                    persistenceService.save(newAuthority);
                } catch (Exception e) {
                    status.setRollbackOnly();
                    log.error(e.getMessage(), e);
                    return result(Action.ERROR, getText("registration.error"));
                }
                
                return result(Action.SUCCESS, getText("registration.success"));
            }
        });
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void validate() {
        super.validate();
        
        if(userUtils.userExists(username)) {
            addFieldError("username", getText("registration.username.exists"));
        }
    }

	public PersistenceService getPersistenceService() {
		return persistenceService;
	}

	public void setPersistenceService(PersistenceService persistenceService) {
		this.persistenceService = persistenceService;
	}
	
    public TransactionService getTransactionService() {
        return transactionService;
    }

    public void setTransactionService(TransactionService transactionService) {
        this.transactionService = transactionService;
    }

    public UserUtils getUserUtils() {
        return userUtils;
    }

    public void setUserUtils(UserUtils userUtils) {
        this.userUtils = userUtils;
    }
}
